springboot拦截器使用说明 您所在的位置:网站首页 springboot restful 非法调用 springboot拦截器使用说明

springboot拦截器使用说明

2023-06-11 04:56| 来源: 网络整理| 查看: 265

Spring MVC中,定义的拦截器必须实现HandlerInterceptor接口或其子接口。HandlerInterceptor接口定义了 三个方法:preHandle、postHandle和afterCompletion,这些方法为拦截器的执行提供了三个不同的阶段。 如果定义的拦截器没有实现HandlerInterceptor接口或其子接口,那么这个拦截器实际上就不是拦截器,仅仅是一个 普通的Java类,并不能在Spring MVC的拦截器链中起到拦截器的作用。因此,实现HandlerInterceptor接口 或其子接口是必须的。另外,为了将拦截器加入Spring MVC的拦截器链中,还需要在配置类中通过addInterceptor 方法将拦截器添加进去。在addInterceptor方法中,需要传递一个实现了HandlerInterceptor接口或其子接口的 拦截器实例。通过这种方式,才能让拦截器真正地在请求处理过程中起到拦截和处理的作用。 15.4 mybatis-plus分页插件的拦截器

  @Bean   public MybatisPlusInterceptor mybatisPlusInterceptor() {   MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();   interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));   return interceptor;   }

这段代码中的MybatisPlusInterceptor是Mybatis-Plus插件中的拦截器,不是Spring MVC框架中的拦截器。 这个拦截器不需要实现HandlerInterceptor接口,因为它的作用是拦截Mybatis的SQL执行过程而非Spring MVC 框架中的请求处理过程。当我们使用Mybatis-Plus的分页插件时,可以配置MybatisPlusInterceptor来实现 分页功能。MybatisPlusInterceptor是一个拦截器,负责拦截Mybatis执行SQL的过程,它在SQL执行前将分页 参数注入到SQL中,在SQL执行后将查询到的数据进行分页处理和封装返回结果。因此,在这段代码中,虽然 MybatisPlusInterceptor不需要实现HandlerInterceptor接口,但它的作用和Spring MVC框架中的拦截器 有所不同,主要是在Mybatis的SQL执行过程中拦截对SQL的处理。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有